package com.bytecodezz.app.base.extend.function

import android.app.Application
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/8/19 10:33
 * @package: com.bytecodezz.app.base.extend.function
 *
 * @description:
 *
 */
//fun Application.getAppMetaData(key: String): String? {
//    var resultData: String? = null
//    try {
//        val applicationInfo =
//            packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
//        applicationInfo.metaData?.let {
//            resultData = it.getString(key)
//        }
//    } catch (e: PackageManager.NameNotFoundException) {
//        e.printStackTrace()
//    }
//    return resultData
//}
//
//fun Application.getVersionName(): String {
//    // getPackageName()是你当前类的包名，0代表是获取版本信息
//    var version = "0"
//    try {
//        packageManager.getPackageInfo(packageName, 0)?.let {
//            version = it.versionName
//        }
//    } catch (e: PackageManager.NameNotFoundException) {
//        // 没找到
//    }
//    return version
//}

fun Context.getServiceExplicitIntent(implicitIntent: Intent): Intent? {
    val pm = packageManager
    val resolveInfoList = pm.queryIntentServices(implicitIntent, 0)
    if (resolveInfoList == null || resolveInfoList.size != 1) {
        return null
    }
    var explicitIntent: Intent?
    val info = resolveInfoList[0]
    val packageName = info.serviceInfo.packageName
    val className = info.serviceInfo.name
    val component = ComponentName(packageName, className)
    explicitIntent = Intent(implicitIntent)
    explicitIntent.component = component
    return explicitIntent
}

